<?php
namespace app\admin\command;

use think\Hook;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Log;
use fast\Random;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

use addons\yungift\library\Super; 
use addons\yungift\library\Constant; 
use addons\yungift\model\ExpressInfo;
use app\admin\model\yungift\GiftSpec;

class Test extends Command
{
    protected function configure()
    {   
        $this->setName('test')
            ->setDescription('test');
    }   

    protected function execute(Input $input, Output $output)
    {   
        // $this->main(); 
        // $this->getExpress();
        // $this->getExpressInfo();
        $worth1 = 190;
        $worth2 = 1000000;
        $specQuery = new GiftSpec();
        
        // 输出SQL语句进行调试
        $specModel = $specQuery->where('worth', '>=', $worth1)
                 ->where('worth', '<=', $worth2);
        // echo "SQL: " . $specModel->fetchSql(true)->select() . "\n";
        
        // 获取并输出结果数量
        $result = $specModel->select();
        echo "查询结果数量: " . count($result) . "\n";
        
        foreach ($result as $s) {
            echo "gift_id: " . $s->gift_id . "\n";
        }

        $output->info('fix success');
    }   

    private function getExpressInfo()
    {
        $expressInfos = Constant::EXPRESS_INFOS; 
        $expressArray = json_decode($expressInfos, true);
        foreach ($expressArray as $code => $express) {
            $expressModel = new ExpressInfo();
            $expressModel->express_code = $code;
            $expressModel->express_name = $express;
            $expressModel->save();
        }
    }

    private function getExpress()
    {
        $appcode = 'bf75f74938c2422aab6d68ec02cec05b';
        $api = 'https://wdexpress.market.alicloudapi.com/globalExpressLists';
        $response = Super::requestAliCloudApi($appcode, $api, 'GET', ['type' => 'ALL']);
        var_dump($response);
        $response = json_decode($response, true);
    }

    private function createClient(){
        $config = new Config([
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
            "accessKeyId" => 'LTAI5t7mcrPnWK4Q6TAN5fV8',
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            "accessKeySecret" => 'oJnDH7YkOoFvlaRbqfqqcWagZcg2OV',
        ]);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    private function main(){
        $client = $this->createClient();
        $sendSmsRequest = new SendSmsRequest([
            "phoneNumbers" => "13764628924",
            "signName" => "广东南油外服",
            "templateCode" => "SMS_478490255",
            "templateParam" => "{\"code\":\"123456\"}"
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $response = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
            var_dump($response->toMap());
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            var_dump($error->message);
            // 诊断地址
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }
    }
}
